package sword.chapter11BinarySearch;

/**
 * 查找插入位置
 *
 * @author K
 * @date 2021-12-21 22:27
 */
public class S68SearchInsert {

    public static int s68(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (nums[mid] >= target) {
                if (mid == 0 || nums[mid - 1] < target) {
                    return mid;
                }
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return nums.length;
    }

    public static void main(String[] args) {
        int[] nums = {2, 3, 5, 8};
        int result = s68(nums, 1);
        int result2 = s68(nums, 3);
        int result3 = s68(nums, 4);
        System.out.println();
    }
}
